package com.touch.client.forms;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;

import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;

import com.touch.client.ClientControls;
import com.touch.client.ClientSocketEvents;
import com.touch.jgloo.owt.JGLButton;
import com.touch.jgloo.owt.JGLComponent;
import com.touch.jgloo.owt.JGLFrame;
import com.touch.jgloo.owt.JGLLabel;
import com.touch.jgloo.owt.JGLTextBox;

public class LoginGLEvents implements GLEventListener, MouseListener, KeyListener
{
	public JGLFrame fLogin;		
	protected JGLTextBox tUsername;
	protected JGLTextBox tPassword;
	protected JGLLabel lUsername;
	protected JGLLabel lPassword;
	protected JGLButton bConnect;
	protected JGLButton bCancel;	
	
	public void display(GLAutoDrawable arg0) 
	{	
		fLogin.render();
	}

	public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) {
		// TODO Auto-generated method stub
		
	}

	public void init(GLAutoDrawable arg0) 
	{	
		//build the login menu
		fLogin = new JGLFrame("Login", 320, 240);	
		bCancel = new JGLButton("Cancel", 120, 25);
		bConnect = new JGLButton("Connect", 120, 25);
		tPassword = new JGLTextBox(120, "          ");
		tUsername = new JGLTextBox(120, "          ");
		lPassword = new JGLLabel("Password");
		lUsername = new JGLLabel("Username");
		
		fLogin.add(tUsername);
		fLogin.add(tPassword);
		fLogin.add(lUsername);
		fLogin.add(lPassword);
		fLogin.add(bCancel);
		fLogin.add(bConnect);
		
		lUsername.setPosition(30, 60, 0);
		tUsername.setPosition(120, 60, 0);
		lPassword.setPosition(30, 120, 0);
		tPassword.setPosition(120, 120, 0);		
		bConnect.setPosition(30, 200, 0);
		bCancel.setPosition(170, 200, 0);	
	}

	public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) 	{}
	public void mouseClicked(MouseEvent e) 
	{	
		fLogin.mouseClicked(e);
		tUsername.mouseClicked(e);
		tPassword.mouseClicked(e);
		bConnect.mouseClicked(e);
		bCancel.mouseClicked(e);
		
		if(JGLComponent.getSelected() ==  bConnect.getIndex())
		{
			String name = tUsername.getText().trim();
			String password = tPassword.getText().trim();
			
			if(name == null || password == null)
				return;
			if(name.length() < 3 || password.length() < 3)
				return;			
			byte[] message = ClientSocketEvents.LOGIN_PACKET(name.getBytes(), password.getBytes());
			
			ClientControls.CLIENT_SOCKET.send(message);		
		}
		else if(JGLComponent.getSelected() == bCancel.getIndex())
		{					
			fLogin.setVisible(false);		
		}		
	}

	public void mouseEntered(MouseEvent arg0) 	{}
	public void mouseExited(MouseEvent arg0) 	{}
	public void mousePressed(MouseEvent e) 
	{	
		fLogin.mousePressed(e);
	}
	public void mouseReleased(MouseEvent e) 
	{
		fLogin.mouseReleased(e);
	}
	public void keyPressed(KeyEvent e) 
	{	
		tUsername.keyPressed(e);
		tPassword.keyPressed(e);
	}
	public void keyReleased(KeyEvent e) 
	{	
		tUsername.keyReleased(e);
		tPassword.keyReleased(e);
	}
	public void keyTyped(KeyEvent e) 
	{
		tUsername.keyTyped(e);
		tPassword.keyTyped(e);
	}
}
